package cn.ncist.health_backend.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/getUsername")
    public RespResult getUsername(){

        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new RespResult(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
